package view;

import dao.Stu_CurriculumDAO;
import dao.Training_ProgramDAO;
import domain.Class_Info;
import domain.Stu_Curriculum;
import domain.Student;
import domain.Teacher;
import service.*;
import utils.Utility;

import java.util.List;

/**
 * @author 杨森
 * @date 2022年11月28日 18:18
 * 教务系统的主页面
 */
public class ManageView {
    //控制是否退出菜单
    private boolean loop = true;
    //接收用户的选择
    private String key = "";
    //定义TeacherService属性
    private TeacherService teacherService = new TeacherService();
    //定义StudentService属性
    private StudentService studentService = new StudentService();
    //定义class_InfoService属性
    private Class_InfoService class_InfoService = new Class_InfoService();
    //定义Stu_CurriculumService属性
    private Stu_CurriculumService stu_CurriculumService=new Stu_CurriculumService();
    //定义Training_ProgramService属性
    private Training_ProgramService training_ProgramService=new Training_ProgramService();

    public void mainMenu() {

        System.out.println("\n===============教务系统================");
        System.out.println("\t\t1：教师登录");
        System.out.println("\t\t2：学生登录");
        System.out.println("\t\t0：退出登录");
        System.out.println("请输入你的选择：");
        key = Utility.readString(1);
        while (loop) {
            switch (key) {
                case "1":
                    tealogin();
                    break;
                case "2":
                    stulogin();
                    break;
                case "0":
                    loop = false;
                    break;
                default:
                    System.out.println("你输入有误，请重新输入.");

            }


        }
        System.out.println("你退出了教务系统~");
    }

    //显示教师个人信息
    private void showTacherInfo() {
        List<Teacher> teacherstList = teacherService.list();
        System.out.println("\n教师号\t\t姓名\t\t性别\t\t\t出生日期\t\t\t班级\t\t教学课程");
        for (Teacher teacher : teacherstList) {
            System.out.println(teacher);
        }
        System.out.println("==============显示完毕============");
    }

    //显示学生个人信息
    private void showStudentInfo() {
        List<Student> studentList = studentService.list();
        System.out.println("\n学号\t\t姓名\t\t性别\t\t\t出生日期\t\t\t班级\t\t选修课程");
        for (Student student : studentList) {
            System.out.println(student);
        }
        System.out.println("==============显示完毕============");
    }

    //显示班级学生
    public void showClassmates(String s_name) {
        List<Student> studentList = studentService.list();
        System.out.println("\n学号\t\t姓名\t\t性别\t\t\t出生日期\t\t\t班级\t\t选修课程");
        for (Student student : studentList) {
            List<Student> studentList1 = studentService.getClassMates(student.getS_class());
            for (Student classMates : studentList1) {
                System.out.println("班级同学"+classMates);
            }
        }
        System.out.println("==============显示完毕============");

    }

    //显示课程情况
    public void showCourseInfo(String t_name) {
        List<Teacher> teacherstList = teacherService.list();
        System.out.println("教学课程");
        for (Teacher teacher : teacherstList) {
            System.out.println(teacher.getT_course());
        }
        System.out.println("==============显示完毕============");

    }

    //显示学生课程情况
    public void showStuCourseInfo(String s_name){
        List<Student> studentCoursetList = studentService.stu_Course_List(s_name);
        System.out.println("学习课程");
        for (Student student :studentCoursetList) {
            System.out.println(student.getS_course());
        }
        System.out.println("==============显示完毕============");

//        System.out.println(studentCoursetList);
    }

    //开设新的课程
    public void newCourse(String t_name){
        System.out.println("==============开设新课程============");
        System.out.print("请选择要开设新课程的编号(-1退出): ");
        int newCourseId = Utility.readCourseId();
        if (newCourseId == -1) {
            System.out.println("=============取消开设============");
            return;
        }
        //验证课程号是否存在
        Training_ProgramDAO training = training_ProgramService.getCourseId(newCourseId);
        if (training != null) {
            System.out.println("=============课程已经存在============");
            return;
        }
        System.out.print("请选择要开设新课程的名称(-1退出): ");
        String newCourseName = Utility.readString(10);
        if(training_ProgramService.createNewCourse(t_name,newCourseName)){
            System.out.println("==============开设新课成功============");
        } else {
            System.out.println("==============开设新课失败============");
        }


    }

    //加入课程
    public void join_Course(String s_name){
        System.out.print("请输入要加入课程的编号(-1退出): ");
        int joinCourseId = Utility.readCourseId();
        if (joinCourseId == -1) {
            System.out.println("=============取消加入============");
            return;
        }
        System.out.print("请输入要加入课程的名称(-1退出): ");
        String joinCourseName = Utility.readString(10);
        if (joinCourseName.equals("-1")) {
            System.out.println("=============取消加入============");
            return;
        }
        System.out.print("请输入你的学号(-1退出): ");
        int s_id = Utility.readCourseId();
        if (joinCourseId == -1) {
            System.out.println("=============取消加入============");
            return;
        }
        if(stu_CurriculumService.insert_Stu(joinCourseId,joinCourseName,s_id,s_name)){
            System.out.println("==============加入新课成功============");
        } else {
            System.out.println("==============加入新课失败============");
        }
    }

    //修改课程信息
    public void updateCourse(){
        System.out.print("请输入要修改课程的编号(-1退出): ");
        int c_id = Utility.readCourseId();
        if (c_id == -1) {
            System.out.println("=============取消输入============");
            return;
        }
        System.out.print("请输入要修改后课程的名称(-1退出): ");
        String c_name = Utility.readString(10);
        if(training_ProgramService.updateCourse(c_name,c_id)){
            System.out.println("==============修改课程成功============");
        } else {
            System.out.println("==============修改课程失败============");
        }
    }

    //根据课程编号来修改分数
    public void updateScore(){
        System.out.print("请输入学生的课程编号(-1退出): ");
        int  c_id= Utility.readCourseId();
        if (c_id == -1) {
            System.out.println("=============取消输入============");
            return;
        }
        System.out.print("请输入学生的成绩(-1退出): ");
        int  score= Utility.readCourseId();
        if (score == -1) {
            System.out.println("=============取消输入============");
            return;
        }

        stu_CurriculumService.updateScore(score,c_id);
    }

    //查询分数
    public void selectScore(String name){
        List<Stu_Curriculum> score=stu_CurriculumService.scoreList(name);
        System.out.println("课程名："+"\t\t"+"分数");
        for (Stu_Curriculum Score :score) {
            System.out.println(Score);
        }

    }

    public void tealogin() {
        System.out.print("输入教师名: ");
        String t_name = Utility.readString(50);
        System.out.print("输入密  码: ");
        String t_pwd = Utility.readString(50);
        Teacher teacher = teacherService.getTeacherByNameAndPwd(t_name, t_pwd);
        if (teacher != null) {
            System.out.println("===============[" + teacher.getT_name() + "]登录成功================\n");
            //显示二级菜单, 这里二级菜单是循环操作，所以做成while
            while (loop) {
                System.out.println("\n===============教务系统(" + teacher.getT_name() + "页面)================");
                System.out.println("\t\t 1 显示个人信息");
                System.out.println("\t\t 2 显示班级学生");
                System.out.println("\t\t 3 查询课程情况");
                System.out.println("\t\t 4 开设新的课程");
                System.out.println("\t\t 5 修改课程信息");
                System.out.println("\t\t 6 学生分数录入");
                System.out.println("\t\t 0 退出教务系统");
                System.out.print("请输入你的选择: ");
                key = Utility.readString(1);
                switch (key) {
                    case "1":
//                        System.out.println("\t\t 1 显示个人信息");
                        showTacherInfo();
                        break;
                    case "2":
                        System.out.println("\t\t 2 显示班级学生");
//                        showClassmates();
                        break;
                    case "3":
//                        System.out.println("\t\t 3 查询课程情况");
                        showCourseInfo(t_name);
                        break;
                    case "4":
//                        System.out.println("\t\t 4 开设新的课程");
                        //有bug 插入的中文课程会变为？？
                        newCourse(t_name);
                        break;
                    case "5":
//                        System.out.println("\t\t 5 修改课程信息");
                        updateCourse();
                        break;
                    case "6":
//                        System.out.println("\t\t 6 学生分数录入");
                        updateScore();
                        break;
                    case "0":
                        System.out.println("\t\t 0 退出教务系统");
                        loop = false;
                        break;
                    default:
                        System.out.println("你的输入有误，请重新输入");
                        break;

                }
            }
        } else {
            System.out.println("===============登录失败================");
        }
    }


    public void stulogin() {
        System.out.print("输入学生名: ");
        String s_name = Utility.readString(50);
        System.out.print("输入密  码: ");
        String s_pwd = Utility.readString(50);
        Student student = studentService.getStudentByNameAndPwd(s_name, s_pwd);
        if (student != null) {
            System.out.println("===============[" + student.getS_name() + "]登录成功================\n");
            //显示二级菜单, 这里二级菜单是循环操作，所以做成while
            while (loop) {
                System.out.println("\n===============教务系统(" + student.getS_name() + "页面)================");
                System.out.println("\t\t 1 显示个人信息");
                System.out.println("\t\t 2 显示班级学生");
                System.out.println("\t\t 3 查询课程情况");
                System.out.println("\t\t 4 加入新的课程");
                System.out.println("\t\t 5 学生分数查询");
                System.out.println("\t\t 0 退出教务系统");
                System.out.print("请输入你的选择: ");
                key = Utility.readString(1);
                switch (key) {
                    case "1":
//                        System.out.println("\t\t 1 显示个人信息");
                        showStudentInfo();
                        break;
                    case "2":
//                        System.out.println("\t\t 2 显示班级学生");
                        showClassmates(s_name);
                        break;
                    case "3":
//                        System.out.println("\t\t 3 查询课程情况");
                        showStuCourseInfo(s_name);
                        break;
                    case "4":
//                        System.out.println("\t\t 4 加入新的课程");
                        join_Course(s_name);
                        break;
                    case "5":
//                        System.out.println("\t\t 5 学生分数查询");
                        selectScore(s_name);
                        break;
                    case "0":
                        System.out.println("\t\t 0 退出教务系统");
                        loop = false;
                        break;
                    default:
                        System.out.println("你的输入有误，请重新输入");
                        break;

                }
            }
        } else {
            System.out.println("===============登录失败================");
        }
    }

    public static void main(String[] args) {
        new ManageView().mainMenu();
    }
}
